﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL.WorkingCenter;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.Model.Entity.WorkingCenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.Mannage.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class ProductRelatedController : BaseController
    {
        private readonly ProductInfoRelatedBLL productInfoRelatedBLL;

        public ProductRelatedController(ProductInfoRelatedBLL productInfoRelatedBLL,
           ILogger<ProductRelatedController> logger) : base(logger)
        {
            this.productInfoRelatedBLL = productInfoRelatedBLL;
        }

        [HttpPost("Save")]
        public ProductInfoRelatedDto Save(ProductInfoRelatedEntity info)
        {
            ProductInfoRelatedDto dto = new ProductInfoRelatedDto();
            try
            {
                dto.data = this.productInfoRelatedBLL.Save(info);
            }
            catch (Exception ex)
            {
                dto = new ProductInfoRelatedDto();
                dto.SetException(ex);
                base.WriteException("ProductRelated=>Save", ex);
            }

            return dto;
        }

        [HttpGet("Delete")]
        public DataDto Delete(int SN)
        {
            DataDto dto = new DataDto();
            try
            {
                int iResult = this.productInfoRelatedBLL.Delete(SN);
                if (iResult == 0)
                {
                    dto.error = new Error()
                    {
                        code = -1,
                        msg = "成功行数为0"
                    };
                }
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("ProductRelated>>Save", ex);
            }

            return dto;
        }

        [HttpGet("List")]
        public ProductInfoRelatedListDto List(long productVSN)
        {
            ProductInfoRelatedListDto dto = new ProductInfoRelatedListDto();
            try
            {
                dto.data = this.productInfoRelatedBLL.QueryList(productVSN);
            }
            catch (Exception ex)
            {
                dto = new ProductInfoRelatedListDto();
                dto.SetException(ex);
                base.WriteException("ProductRelated>>List", ex);
            }

            return dto;
        }
    }
}
